<template>
	<view>
		<u-navbar :title="$t('确认订单')" background="{ background: '#fff' }" title-color="#000" back-icon-color="#000"></u-navbar>
		<view class="wrap">
			<view class="address" @click="$.to('/pagesA/pages/my/address?state=1')" v-if="info.goods_type==1">
				<view class="left">
					<image src="../../../static/image/a1_.png" mode=""></image>
					<view v-if="!isAdd">{{ $t('添加收货地址') }}</view>
					<view class="info" v-else>
						<view class="name">
							{{address.fullname}} <text>{{address.mobile}}</text>
						</view>
						<view class="add">
							{{address.province + ' ' + address.city + ' ' + address.area + ' ' + address.detailed}}
						</view>
					</view>
				</view>
				<u-icon name="arrow-right" color="#ccc"></u-icon>
			</view>

			<view class="goodsBox">
				<view class="goods">
					<image :src="info.image" mode=""></image>
					<view class="goodsInfo">
						<view class="tit">{{info.name}}</view>
						<!-- <view class="xinghao">
							型号：128GB;颜色：暗紫色（请勿现金支付）
						</view> -->
						<view class="price" style="margin-top: 20rpx;" v-if="info.type == 1">￥{{info.price}}</view>
						<view class="price" style="margin-top: 20rpx;color: #fc7802;" v-else>{{info.integral}}{{ $t("糯米值") }}</view>
					</view>
				</view>
				<view class="row">
					<view>{{ $t("购买数量") }}</view>
					<view>x{{info.num}}</view>
				</view>
				<view class="row">
					<view>{{ $t("运费") }}</view>
					<view class="hui" v-if="info.freight_value == 0">{{ $t("免运费") }}</view>
					<view class="hui" v-else>￥{{info.freight_value}}</view>
				</view>
				<!-- <view class="row">
					<view>优惠券</view>
					<view class="hui n-flex-row">
						暂无可使用优惠券
						<u-icon name="arrow-right" ></u-icon>
					</view>
				</view> -->
				<view class="row">
					<view>{{ $t("商品总价") }}</view>
					<view class="pri">￥{{info.price * info.num}}</view>
				</view>
				<view class="row">
					<view>{{ $t("糯米值价格") }}</view>
					<view class="pri">￥{{info.integral * info.num}}</view>
				</view>
				<view class="beizhu">
					<view class="">{{ $t("备注信息") }}</view>
					<u-input v-model="remarks" type="textarea" height="50" :placeholder="$t('选填备注信息')" />
				</view>
			</view>

			<view class="box1" style="margin-top: 20rpx;" v-if="info.type == 1">
				<u-radio-group style="display: block;" width="34rpx" v-model="paymode" active-color="#000">
					<!-- #ifdef MP-WEIXIN || H5 -->
					<view class="rowitem payrow" @click="selectPay('wechat')">
						<view class="l">
							<image src="../../../static/image/weixinf.png" mode=""></image>
							{{ $t("微信") }}
						</view>
						<u-radio name="wechat"></u-radio>
					</view>
					<!-- #endif -->
					<!-- #ifdef APP-PLUS || H5 -->
					<view class="rowitem payrow" @click="selectPay('alipay')">
						<view class="l">
							<image src="../../../static/image/zhifubao.png" mode=""></image>
							{{ $t("支付宝") }}
						</view>
						<u-radio name="alipay"></u-radio>
					</view>
					<!-- #endif -->
					<view class="rowitem payrow" @click="selectPay('integral')">
						<view class="l">
							<image src="../../../static/image/xr.png" mode=""></image>
							{{ $t("糯米值") }}
						</view>
						<u-radio name="integral"></u-radio>
					</view>
				</u-radio-group>
			</view>
		</view>
		<view class="rule">
			<rich-text :nodes="rule"></rich-text>
		</view>
		<view style="height: 200rpx;">
			
		</view>
		<view class="btm">
			<view class="btmbox">
				<view class="n-flex-row n-align-center">
					<view class="">
						<view v-if="info.type == 1">{{$t('共')}}{{info.num}}{{$t('件')}} | {{$t('合计')}}：￥<text class="text">{{price_zj}}</text></view>
						<view v-else>{{ $t("共") }}{{info.num}}{{ $t("件") }} | {{ $t("合计") }}：<text class="text" style="color: #fc7802;">{{info.price * info.num}}</text> <text style="color: #fc7802;">{{ $t("糯米值") }}</text></view>
						<view class="yun" v-if="info.freight_value != 0 && info.goods_type==1">({{ $t("含运费") }}￥{{info.freight_value}})</view>
					</view>
					<button @click="submit">{{ $t("提交订单") }}</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				remarks: '',
				paymode: '',
				info: '',
				address: '',
				isAdd: false, //是否有地址
				// #ifdef MP-WEIXIN || H5
				paymode:'wechat',
				// #endif
				
				// #ifdef APP-PLUS
				paymode:'alipay',
				// #endif
				price_zj:0,
				rule:'',
			}
		},
		onLoad() {
			this.info = uni.getStorageSync('goods')
			if(this.info.goods_type==1){
				this.getAddress()
				uni.$on('changeAddress', (addId) => {
					console.log(addId, 'id');
					this.upDataAddress(addId);
				})
			}
			this.price_zj = ((this.info.price * this.info.num) + Number(this.info.freight_value));
			this.getRule()
		},
		onUnload() {
			uni.removeStorageSync('goods')
			uni.$off('changeAddress');
		},
		methods: {
			// 钱购买
			submit() {
				if (!this.address && this.info.goods_type==1) {
					this.$tip.tip("请选择地址")
					return
				}
				if (this.paymode == "") {
					this.$tip.tip("请选择支付方式")
					return
				}
				this.$http({
					url: "api/pay/go_goods_adapay",
					data: {
						goods_id: this.info.id,
						num: this.info.num,
						paymode: this.paymode,
						desc: this.remarks,
						address_id: this.address.id,
						client: 1
					}
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						this.fun.go_mpwxpay(res.data.data.expend.pay_info).then(payres => {
							this.$tip.tip("支付成功", "success")
							setTimeout(() => {
								uni.redirectTo({
									url: '/pagesB/pages/mall/paySuccexx'
								})
							}, 700)
						}).catch(payerr => {
							this.$tip.tip('支付失败')
						});
					}else if(res.data.code == 8){ //0元免支付的
						uni.redirectTo({
							url: '/pagesB/pages/mall/paySuccexx'
						})
					} else {
						this.$tip.tip(res.data.msg)
					}
				
				}).catch(err => {});
				return;
				
			},
			getRule(){
				this.$http({
					url: "api/common/get_agreement",
					data: {
						name: 'delivery_rule'
					}
				}).then(res => {
					if (res.data.code == 1) {
						this.rule = res.data.data.content
					} else {
						this.$tip.tip(res.data.msg)
					}
				}).catch(err => {});
			},
			// 更新收货地址
			upDataAddress(id) {
				this.$http({
					url: "api/member/get_address",
					data: {
						address_id: id
					}
				}).then(res => {
					console.log(res, "dizhi");
					if (res.data.code == 1) {
						this.address = res.data.data
						this.isAdd = true
					}

				}).catch(err => {});
			},
			getAddress() {
				this.$http({
					url: "api/member/my_address",
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						if (res.data.data.length == 0) {
							this.isAdd = false
						} else {
							this.isAdd = true
						}
						this.address = res.data.data[0]
					} else {
						this.$tip.tip(res.data.msg)
						this.isAdd = false
					}
				}).catch(err => {});
			},
			// 选择支付方式
			selectPay(mode) {
				this.paymode = mode
				if(mode=='integral'){
					this.price_zj = ((this.info.integral * this.info.num) + Number(this.info.freight_value))
				}else{
					this.price_zj = ((this.info.price * this.info.num) + Number(this.info.freight_value))
				}
			},
		}
	}
</script>

<style>
	@import url("../../../static/css/confirmOrder.css");
</style>
